var express = require('express');
const multer=require('multer');
var router = express.Router();
const Blog = require('../connet/mongod')
// 配置 multer 存储引擎  
const storage = multer.diskStorage({  
  destination: (req, file, cb) => {  
    // 指定文件存储的目录  
    cb(null, './public/images');  
  },  
  filename: (req, file, cb) => {  
    // 为上传的文件指定一个唯一的文件名  
    const date = Date.now();  
    cb(null, `${date}.png`);  
  }  
});  
const upload = multer({ storage: storage }); 
// 文章的上传图片
router.post('/',upload.single('file'), async (req, res)=>{
  if (!req.file) {
    res.status(400).send({message: 'No file uploaded'});
    return;
  } 
  const filename = req.file.filename;
  console.log(filename, req.file, 'filename');
  const url = `http://localhost:3001/images/${filename}`;
  const alt =  filename;
  const href = `http://localhost:3001/images/${filename}`;
  res.status(200).send({    
  "errno": 0, // 注意：值是数字，不能是字符串
  "data": {
    url, // 图片 src ，必须
    alt, // 图片描述文字，非必须
    href // 图片的链接，非必须
  },isok:true})
})
// // 删除
// router.post('/del', async function(req, res){
//     const _id = req.body.id
//     const data = await Blog.like.deleteOne(_id);
//     res.status(200).send({data,isok:true})
//   })

module.exports = router;
